package com.ssm.controller;

import com.ssm.model.User;
import com.ssm.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zhangxq on 2016/7/15.
 */

@Controller
@RequestMapping("/user")
public class UserController {

    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @RequestMapping("/showUser")
    public String showUser(HttpServletRequest request, Model model){
        log.info("查询所有用户信息");
        List<User> userList = userService.getAllUser();
        model.addAttribute("userList",userList);
        return "showUser";
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public @ResponseBody List<User> list(@RequestParam("id") Integer id, String name, HttpRequest request){
        String token = request.getHeaders().get("token").get(0);
        System.out.println(token);
        System.out.println(id+name);
        log.info("查询所有用户信息");
        return userService.getAllUser();
    }

    @RequestMapping(value = "/find",method = RequestMethod.POST)
    public @ResponseBody List<User> find(@RequestBody User user){
        log.info("查询用户信息");
        System.out.println(user.toString());
        return userService.getAllUser();
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public @ResponseBody List<String> upload(@RequestParam("files") MultipartFile[] files,String obj){
        List<String> fileList = new ArrayList<>(files.length);
        for(MultipartFile m:files){
            String f = "文件名： "+m.getOriginalFilename()+" 名称: "+m.getName()+" 大小： "+m.getSize();
            System.out.println(m);
            fileList.add(f);
        }
        fileList.add("普通表单数据： "+obj);
        return fileList;
    }
}
